﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
using Delay;
using System.Diagnostics;

namespace PhoneInfo.Page
{
    public partial class userManager : PhoneApplicationPage
    {
        private ObservableCollection<userTable> _accountList;
        public userManager()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (IsEmptyDB() == true)
            {//添加元素
                userTable temp = new userTable()
                {
                    UserName = "添加新账号",
                    Type = "Tail",
                    PassWord = ""
                };
                try
                {
                    App.ManagerDB.accountTable.InsertOnSubmit(temp);
                    App.ManagerDB.SubmitChanges();
                }
                catch (Exception e1)
                {
                    Debug.WriteLine("Exception message is {0}", e1.Message);
                    Debug.WriteLine("Exception data is {0}", e1.Data);
                }
            }
            _accountList = new ObservableCollection<userTable>(loadDB());
            accountManager.DataContext = _accountList;
        }
        private bool IsEmptyDB()
        {
            var userInDB = from userTable user in App.ManagerDB.accountTable
                           select user;
            foreach (var item in userInDB)
            {
                return false;
            }
            return true;
        }
        private List<userTable> loadDB()
        {
            var userInDB = (from userTable user in App.ManagerDB.accountTable
                            orderby user.Type
                            select user).ToList();
            return userInDB;
        }
        private void DeleteAccount(object sender, RoutedEventArgs e)
        {
            //删除账号
            MenuItem lb = sender as MenuItem;
            if (lb == null) return;
            userTable table = lb.DataContext as userTable;
            _accountList.Remove(table);
            App.ManagerDB.accountTable.DeleteOnSubmit(table);
            App.ManagerDB.SubmitChanges();
        }
        private void accountManager_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (sender == null) return;
            ListBox lb = sender as ListBox;
            if (lb == null) return;
            userTable table = lb.SelectedItem as userTable;
            if (table.Type == "Tail")
            {//添加新账号
                string objective = "/Page/RegisterAccount.xaml";
                NavigationService.Navigate(new Uri(objective, UriKind.Relative));
            }
            else if (table.Type == "Main")
            {//将Main类型编程Head 将Head类型编程Main
                foreach (userTable item in _accountList)
                {
                    if (item.Type == "Head") item.Type = "Main";
                }
                table.Type = "Head";
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button bt = sender as Button;
            if (bt == null) return;
            userTable table = bt.DataContext as userTable;
            _accountList.Remove(table);
            App.ManagerDB.accountTable.DeleteOnSubmit(table);
            App.ManagerDB.SubmitChanges();
        }
    }
}